package com.ruoyi.omsampuser.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.omsampuser.domain.OmSampuser;
import com.ruoyi.omsampuser.service.IOmSampuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户文章连接Controller
 * 
 * @author ruoyi
 * @date 2025-02-24
 */
@RestController
@RequestMapping("/mp/om/sampuser")
public class MpOmSampuserController extends BaseController
{
    @Autowired
    private IOmSampuserService omSampuserService;

    /**
     * 魔改下查 这个文章 和  用户 是否有值
     *
     */
    @GetMapping(value = "/{sampuserId}")
    public AjaxResult getInfo(@PathVariable("sampuserId") Long sampuserId)
    {
        return success(omSampuserService.selectOmSampuserBySampuserId(sampuserId));
    }

    /**
     * 新增用户文章连接
     * 收藏
     */
    @Log(title = "用户文章连接", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OmSampuser omSampuser)
    {
        return toAjax(omSampuserService.insertOmSampuser(omSampuser));
    }

    // 去掉收藏。 wen1 - user1    wen2  -user2  文章id是唯一的
    /**
     * 删除用户文章连接
     *  根据用户id 和 文章
     */

	@DeleteMapping("/{sampuserIds}")
    public AjaxResult remove(@PathVariable Long[] sampuserIds)
    {
        return toAjax(omSampuserService.deleteOmSampuserBySampuserIds(sampuserIds));
    }



    //收藏 取消收藏
    //collect
    @PostMapping("/collect")
    public AjaxResult collect(@RequestBody OmSampuser omSampuser)
    {
        //先查
        Long saId = omSampuser.getSampuserSaid();
        String openId = omSampuser.getSampuserOpenid();
        OmSampuser omSampuser_db = omSampuserService.getCheckInfo(saId,openId);
        if(null != omSampuser_db){
            return error("已经收藏过，不能再收藏");
        }
        return toAjax(omSampuserService.insertOmSampuser(omSampuser));
    }


    @PostMapping("/uncollect")
    public AjaxResult uncollect(@RequestBody OmSampuser omSampuser)
    {
        return toAjax(omSampuserService.uncollect(omSampuser));
    }


}
